package org.xml.xpath4js.client;

/* 
xpath4js is a GWT implementation of XPath 2.0 
Copyright (C) 2010  Peter Schonefeld (peter dot schonefeld at gmail)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import org.xml.xpath4js.client.*;

import com.google.gwt.xml.client.*;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
@SuppressWarnings("unused")
public class Xpath4js implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		//XPath Test
		XPathSequence input  = new XPathSequence();
		XPathSequence result = new XPathSequence();


		/*
		<mydoc><mynode foo="bar"/><myothernode/></mydoc>
		*/
		
		XPathNode doc, node, node2, attr;

		input.AppendItem(new XPathInput("<mydoc><mynode foo='bar'/><myothernode/></mydoc>").XPathDoc);		
		
		// test1 = "//*"
		result =  EvalEngine.DoXPath(input, "//*");
		HTML test1 = new HTML("Test 1: input --> '//*'; expected --> &lt;mydoc>, &lt;mynode&gt;, &lt;myothernode>; result --> "+result.toHTMLString());
		RootPanel.get().add(test1);
		RootPanel.get().add(new HTML("<br/>"));
		
		// test2 = "//attribute:*"		
		result =  EvalEngine.DoXPath(input, "//attribute:*");
		HTML test2 = new HTML("Test 2: input --> '//attribute:*'; expected --> //<span style='color:red'>attribute:</span>* ; result --> "+result.toHTMLString());
		RootPanel.get().add(test2);
		RootPanel.get().add(new HTML("<br/>"));
		
		// test3 = "//attribute::*"		
		result =  EvalEngine.DoXPath(input, "//attribute::*");
		HTML test3 = new HTML("Test 3: input --> '//attribute::*'; expected --> &lt;myattr&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(test3);
		RootPanel.get().add(new HTML("<br/>"));		
		
		// test3 = "/mynode"		
		result =  EvalEngine.DoXPath(input, "/mydoc/mynode");
		HTML test4 = new HTML("Test 4: input --> '/mydoc/mynode'; expected --> &lt;mynode&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(test4);
		RootPanel.get().add(new HTML("<br/>"));	
		
		// test3 = "/mynode"		
		result =  EvalEngine.DoXPath(input, "/mydoc/mynode/@foo");
		HTML test4b = new HTML("Test 4b: input --> '/mydoc/mynode/@foo'; expected --> &lt;@foo&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(test4b);
		RootPanel.get().add(new HTML("<br/>"));			
		
		// test3 = "/mynode"		
		result =  EvalEngine.DoXPath(input, "/mydoc//*");
		HTML test5 = new HTML("Test 5: input --> '/mydoc//*'; expected --> &lt;mynode&gt;, &lt;myothernode&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(test5);
		RootPanel.get().add(new HTML("<br/>"));		
		
		input.Clear();
		input.AppendItem(new XPathInput("<AAA><BBB/><CCC/><BBB/><BBB/><DDD><BBB/></DDD><CCC/></AAA>").XPathDoc);	
		result =  EvalEngine.DoXPath(input, "/AAA");
		HTML test6 = new HTML("Test 6: input --> '/AAA'; expected --> &lt;AAA&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(test6);
		RootPanel.get().add(new HTML("<br/>"));		
		
		result =  EvalEngine.DoXPath(input, "/AAA/CCC");
		HTML test7 = new HTML("Test 7: xpath --> '/AAA/CCC'; expected --> &lt;CCC&gt;,&lt;CCC&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(test7);
		RootPanel.get().add(new HTML("<br/>"));	

		result =  EvalEngine.DoXPath(input, "/AAA/DDD/BBB");
		HTML test8 = new HTML("Test 8: xpath --> '/AAA/DDD/BBB'; expected --> &lt;BBB&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(test8);
		RootPanel.get().add(new HTML("<br/>"));	
		
		input.Clear();
		input.AppendItem(new XPathInput("<AAA><XXX><DDD><BBB/><BBB/><EEE/><FFF/></DDD></XXX><CCC><DDD><BBB/><BBB/><EEE/><FFF/></DDD></CCC><CCC><BBB><BBB><BBB/></BBB></BBB></CCC></AAA>").XPathDoc);	
		result =  EvalEngine.DoXPath(input, "/AAA/CCC/DDD/*");
		HTML test9 = new HTML("Test 9: xpath --> '/AAA/CCC/DDD/*'; expected --> &lt;BBB&gt;, &lt;BBB&gt;, &lt;EEE&gt;, &lt;FFF&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(test9);
		RootPanel.get().add(new HTML("<br/>"));	

		result =  EvalEngine.DoXPath(input, "/*/*/*/BBB");
		HTML test10 = new HTML("Test 10: xpath --> '/*/*/*/BBB'; expected --> &lt;BBB&gt; x 5 ; result --> "+result.toHTMLString());
		RootPanel.get().add(test10);
		RootPanel.get().add(new HTML("<br/>"));			
		
		input.Clear();
		//input.AppendItem(new XPathInput("<AAA><BBB/><CCC/><CCC/><CCC/></AAA>").XPathDoc);	
		//result =  EvalEngine.DoXPath(input, "/AAA/BBB[1]");
		HTML test11 = new HTML("<span style='color:red'>Test 11: xpath --> '/AAA/BBB[1]'; expected --> &lt;BBB&gt;; result --> runtime error...expressions in predicates not implemented :( eg. [fn:position()=1]</span>");//+result.toHTMLString());
		RootPanel.get().add(test11);
		RootPanel.get().add(new HTML("<br/>"));			
	      
		input.Clear();
		input.AppendItem(new XPathInput("<AAA><BBB id = 'b1'/><BBB id = 'b2'/><BBB name = 'bbb'/><BBB/></AAA>").XPathDoc);	
		result =  EvalEngine.DoXPath(input, "//@id");
		HTML zvon5a = new HTML("<a href='http://www.zvon.org/xxl/XPathTutorial/Output/example5.html'>Zvon 5A</a>: xpath --> '//@id'; expected --> &lt;attr:id&gt;, &lt;attr:id&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(zvon5a);
		RootPanel.get().add(new HTML("<br/>"));	
		
		//input.Clear();
		//input.AppendItem(new XPathInput("<AAA><BBB id = 'b1'/><BBB id = 'b2'/><BBB name = 'bbb'/><BBB/></AAA>").XPathDoc);	
		result =  EvalEngine.DoXPath(input, "//BBB[@id]");
		HTML zvon5b = new HTML("<a href='http://www.zvon.org/xxl/XPathTutorial/Output/example5.html'>Zvon 5B</a>: xpath --> '//BBB[@id]'; expected --> &lt;BBB&gt;, &lt;BBB&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(zvon5b);
		RootPanel.get().add(new HTML("<br/>"));		
		
		input.Clear();
		input.AppendItem(new XPathInput("<AAA><BBB> hello </BBB></AAA>").XPathDoc);	
		result =  EvalEngine.DoXPath(input, "//BBB/text()");
		HTML valueTest1 = new HTML("Select text test 1: xpath --> '//BBB/text()'; expected --> hello ; result --> "+result.toHTMLString());
		RootPanel.get().add(valueTest1);
		RootPanel.get().add(new HTML("<br/>"));	
		
		input.AppendItem(new XPathInput("<AAA><BBB> there </BBB></AAA>").XPathDoc);	
		result =  EvalEngine.DoXPath(input, "//BBB/text()");
		HTML valueTest2 = new HTML("Select text test 2: xpath --> '//BBB/text()'; expected -->&quot; hello &quot;, &quot; there &quot;; result --> "+result.toHTMLString());
		RootPanel.get().add(valueTest2);
		RootPanel.get().add(new HTML("<br/>"));			
		
		input.Clear();
		input.AppendItem(new XPathInput("<chart type='3D' family='pieChart'><def><background width='400' height='160' style='fill:#fff'/><title>This is the title of my chart</title><legend show='true'/></def><data><item><label>A</label><desc>Item A</desc><value>20</value></item><item><label>B</label><desc>Item B</desc><value>13</value></item><item><label>C</label><desc>Item C</desc><value>3</value></item><item><label>D</label><desc>Item D</desc><value>7</value></item></data></chart>").XPathDoc);
		result =  EvalEngine.DoXPath(input, "/chart/data/item");
		HTML test12 = new HTML("test 12: xpath --> '/chart/data/item'; expected -->&lt;item&gt;, &lt;item&gt;, &lt;item&gt;, &lt;item&gt;; result --> "+result.toHTMLString());
		RootPanel.get().add(test12);
		RootPanel.get().add(new HTML("<br/>"));		
		
		input.Clear();
		input.AppendItem(new XPathInput("<AAA><BBB><CCC/></BBB><DDD><EEE/></DDD></AAA>").XPathDoc);
		result = EvalEngine.DoXPath(input, "/AAA/*[CCC]");
		HTML test13 = new HTML("basic predicate: xpath --> '/AAA/*[CCC]'; expected --> &lt;BBB&gt;; result --> "+ result.toHTMLString());
		RootPanel.get().add(test13);
		RootPanel.get().add(new HTML("<br/>"));		
		
		input.Clear();
		input.AppendItem(new XPathInput("<AAA><BBB><CCC/><EEE/></BBB><DDD></DDD></AAA>").XPathDoc);		
		result = EvalEngine.DoXPath(input, "/AAA/*[CCC][EEE]");
		HTML test13b = new HTML("basic predicate: xpath --> '/AAA/*[CCC][EEE]'; expected --> &lt;BBB&gt;; result --> "+ result.toHTMLString());
		RootPanel.get().add(test13b);
		RootPanel.get().add(new HTML("<br/>"));			
		
		//result = EvalEngine.DoXPath(input, "/AAA/BBB/fn:position()");
		//HTML test14 = new HTML("basic func: xpath --> '/AAA/BBB/fn:position()'; expected --> 2 result --> "+ result.toHTMLString());
		//RootPanel.get().add(test14);
		//RootPanel.get().add(new HTML("<br/>"));			

		//result = EvalEngine.DoXPath(new XPathSequence(), "2 * 5");
		//HTML test15 = new HTML("<span style='color:red'>basic op: xpath --> '2 * 5'; expected --> 10; result --> "+ result.toHTMLString() +"</span>");
		//RootPanel.get().add(test15);
		//RootPanel.get().add(new HTML("<br/>"));			
				
		
		//Window.alert(result.GetItemAt(0).DomObject.toString());
		

		
		
	}

}
